package com.gongpingjialoan.activity.pay;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.gongpingjialoan.R;
import com.gongpingjialoan.activity.main.BaseActivity;
import com.gongpingjialoan.api.API;
import com.gongpingjialoan.network.NetDataJson;
import com.gongpingjialoan.widget.LoadingDialog;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * 订金退款申请
 * Created by Chuangcg on 2016/11/15.
 */
public class RefundActivity extends BaseActivity {

    public static final int REFUND_SUCC = 101;
    private String mType;
    private int id;
    private NetDataJson mNetDataJson;
    private NetDataJson mGetPriceNetDataJson;
    private LoadingDialog mLoadingDialog;
    private ImageView[] mCheckImg = new ImageView[5];
    private TextView[] mReason = new TextView[5];
    private LinearLayout[] mResaonLl = new LinearLayout[5];
    private int selectPosition;
    private TextView mReasonTv;
    private TextView mMoney;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_refund);
        setTitle("订金退款申请");
        initView();
        initData();
    }

    @Override
    protected void onDestroy() {
        if (mNetDataJson != null) {
            mNetDataJson.cancelTask();
        }
        if (mGetPriceNetDataJson != null) {
            mGetPriceNetDataJson.cancelTask();
        }
        super.onDestroy();
    }

    private void initData() {
        Intent intent = getIntent();
        if (intent != null) {
            mType = intent.getStringExtra("type");
            id = intent.getIntExtra("car_id", 0);
        }
        getPayPrices();
    }


    private void initView() {
        mLoadingDialog = new LoadingDialog(this);
        mResaonLl[0] = (LinearLayout) findViewById(R.id.reason_ll1);
        mResaonLl[1] = (LinearLayout) findViewById(R.id.reason_ll2);
        mResaonLl[2] = (LinearLayout) findViewById(R.id.reason_ll3);
        mResaonLl[3] = (LinearLayout) findViewById(R.id.reason_ll4);
        mResaonLl[4] = (LinearLayout) findViewById(R.id.reason_ll5);
        mCheckImg[0] = (ImageView) findViewById(R.id.check_img1);
        mCheckImg[1] = (ImageView) findViewById(R.id.check_img2);
        mCheckImg[2] = (ImageView) findViewById(R.id.check_img3);
        mCheckImg[3] = (ImageView) findViewById(R.id.check_img4);
        mCheckImg[4] = (ImageView) findViewById(R.id.check_img5);
        mReason[0] = (TextView) findViewById(R.id.reason1);
        mReason[1] = (TextView) findViewById(R.id.reason2);
        mReason[2] = (TextView) findViewById(R.id.reason3);
        mReason[3] = (TextView) findViewById(R.id.reason4);
        mReason[4] = (EditText) findViewById(R.id.refund_reason);
        mReasonTv = (TextView) findViewById(R.id.refund_reason_tv);
        mMoney = (TextView) findViewById(R.id.money);
        for (int i = 0; i < 5; i++) {
            final int i0 = i;
            mResaonLl[i].setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    selectPosition = i0;
                    if (selectPosition == 4) {
                        mReasonTv.setVisibility(View.VISIBLE);
                        mReason[4].setVisibility(View.VISIBLE);
                    } else {
                        mReasonTv.setVisibility(View.GONE);
                        mReason[4].setVisibility(View.GONE);
                    }
                    for (int j = 0; j < 5; j++) {
                        if (j != i0) {
                            mCheckImg[j].setSelected(false);
                        } else {
                            mCheckImg[j].setSelected(true);
                        }
                    }
                }
            });
        }

        findViewById(R.id.exit_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                postRefundOrder();

            }
        });
    }

    private void postRefundOrder() {
        CharSequence reason = mReason[selectPosition].getText();
        if (TextUtils.isEmpty(reason)) {
            showTips(2, "请填写退款原因");
            return;
        }
        mNetDataJson = new NetDataJson(new NetDataJson.OnNetDataJsonListener() {
            @Override
            public void onDataJsonError(String errorMessage) {
                showTips(4, errorMessage);
                mLoadingDialog.dismiss();
            }

            @Override
            public void onDataJsonUpdate(JSONObject json) {
                mLoadingDialog.dismiss();
                showTips(1, "退款提交成功");
                setResult(REFUND_SUCC);
                finish();
            }
        });
        mNetDataJson.addParam("id", id);
        mNetDataJson.addParam("type", mType);
        mNetDataJson.addParam("reason", reason.toString());
        mNetDataJson.setUrl(API.post_refund_order);
        mNetDataJson.request("post");
        mLoadingDialog.show();
    }

    private void getPayPrices() {
        mGetPriceNetDataJson = new NetDataJson(new NetDataJson.OnNetDataJsonListener() {

            @Override
            public void onDataJsonUpdate(JSONObject json) {
                mLoadingDialog.dismiss();
                JSONObject jsonObject = null;
                try {
                    jsonObject = json.getJSONObject("data");

                    if (jsonObject == null || 0 == jsonObject.length()) {
                        return;
                    }
                    mMoney.setText(String.valueOf(jsonObject.getInt(id+"") / 100.0));
                } catch (JSONException e) {
                    com.gongpingjialoan.utility.Utils.LogD("onDataJsonUpdate JSONException " + e.getMessage());
                }
            }

            @Override
            public void onDataJsonError(String errorMessage) {
                mLoadingDialog.dismiss();
            }

        });
        mLoadingDialog.show();
        if ("car_ordered_record".equals(mType)) {
            mGetPriceNetDataJson.addParam("car_ids", id);
            mGetPriceNetDataJson.addParam("type", "car_ordered_record");
        } else {
            mGetPriceNetDataJson.addParam("promo_car_ids", id);
            mGetPriceNetDataJson.addParam("type", "promo_car_ordered_record");
        }
        mGetPriceNetDataJson.setUrl(API.get_pay_prices);
        mGetPriceNetDataJson.request("get");
    }


}
